// import Vue from "vue";
import Vuex from "vuex";
// import store from "storejs";
import { getListByTree } from '../utils/data'
import createPersistedState from 'vuex-persistedstate'
// Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    historyRouter: JSON.parse(localStorage.getItem("historyRouter")) || [],
    // token: store.get("token"),
    token: '',
    loading: false,
    userInfo: {},
    menu: {
      list: [],
      // 整合数据
      userMenuAuth: [],
    }
  },
  mutations: {
    SET_HIST_ROUTER (state, data) {
      state.historyRouter = data;
      localStorage.setItem("historyRouter", JSON.stringify(data));
    },
    SET_TOKEN (state, token) {
      localStorage.setItem("token", token);
      state.token = token
    },
    SET_LOADING (state, data = true) {
      state.loading = data;
    },
    SET_USER_INFO (state, user) {
      state.userInfo.userId = user.id
      state.userInfo.userName = user.userName
      state.userInfo.userAccount = user.userAccount
      state.userInfo.phone = user.userInfo ? user.userInfo.phone : ''
      state.userInfo.headPath = user.headPath ? user.headPath : ''
      state.userInfo.roleList = user.roleList ? user.roleList : []
      state.userInfo.deptList = user.deptList ? user.deptList : []
    },
    SET_MENU (state, menu) {
      state.menu.list = menu
      state.menu.userMenuAuth = getListByTree(menu)
    }
  },
  actions: {},
  modules: {},
  plugins: [
    createPersistedState({
      key: `XZDJ_REDBOAT_vuex`,
      // storage: {
      //   getItem: key => ls.get(key),
      //   setItem: (key, value) => ls.set(key, value),
      //   removeItem: key => ls.remove(key)
      // }
      storage: window.sessionStorage
    })
  ]
});
